﻿using System.Collections.Generic;

namespace UM_SERVER.Packet
{
    class ProcessorRouter
    {
        private List<IPacketListener> listeners;
        public ProcessorRouter()
        {
            listeners = new List<IPacketListener>();
        }

        /// <summary>
        ///     安装包处理器
        /// </summary>
        /// <param name="listener" type="UM_SERVER.Packet.IPacketListener">
        ///     <para>
        ///         要安装的指定协议包处理器
        ///     </para>
        /// </param>
        public void InstallProcessor(IPacketListener listener)
        {
            listeners.Add(listener);
        }

        /// <summary>
        ///     将传入的指定包找到对应的包处理器，处理该包
        /// </summary>
        /// <param name="pck" type="UM_SERVER.Packet.InPackage">
        ///     <para>
        ///         要处理的传入包
        ///     </para>
        /// </param>
        public void PacketArrived(InPackage pck)
        {
            foreach (IPacketListener listener in listeners)
            {
                //如果此处理器能够处理该包
                if (listener.Accpet(pck))
                {
                    listener.PackageArrived(pck);
                    break;
                }
            }
        }
    }
}
